package com.atguigu.day07;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class Flink05_Connect {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.创建两条流
        DataStreamSource<Integer> numStream = env.fromElements(1,2,3,4,5);
        DataStreamSource<String> abcStrStream = env.fromElements("a", "b", "c", "d","e");

        //TODO 3.使用Connect链接两条流
        ConnectedStreams<Integer, String> connectedStreams = numStream.connect(abcStrStream);

        SingleOutputStreamOperator<String> result = connectedStreams.map(new CoMapFunction<Integer, String, String>() {
            @Override
            public String map1(Integer value) throws Exception {
                return value * value + "";
            }

            @Override
            public String map2(String value) throws Exception {
                return value + "aaaaaaaaaaa";
            }
        });

        result.print();

        env.execute();
    }
}
